//
//  BigDetailKindCollectionCell.m
//  MarianasTrench
//
//  Created by 王晓晨 on 16/3/18.
//  Copyright © 2016年 王爵. All rights reserved.
//

#import "BigDetailKindCollectionCell.h"

@interface BigDetailKindCollectionCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UICollectionView *smallCollection;
@property (nonatomic, strong) UICollectionReusableView *header;/**< 头视图 >*/
@property (nonatomic, strong) NSString *currentId;/**< 传值用id >*/
@property (nonatomic, strong) NSString *tagName;/**< 传值用name >*/

@end

@implementation BigDetailKindCollectionCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createSubViews];
        
    }return self;
}

#pragma mark - 创建子视图
- (void)createSubViews {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.itemSize = CGSizeMake(SCREEN_WIDTH - 2, 78);
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    self.smallCollection = [[UICollectionView alloc] initWithFrame:self.contentView.frame collectionViewLayout:layout];
    [self.contentView addSubview:_smallCollection];
    _smallCollection.backgroundColor = RGBA(233, 231, 228, 1);
    _smallCollection.delegate = self;
    _smallCollection.dataSource = self;
    [_smallCollection registerClass:[SmallDetailKindCollectionCellA class] forCellWithReuseIdentifier:@"SmallDetailKindCollectionCellA"];
    [_smallCollection registerClass:[SmallDetailKindCollectionCell class] forCellWithReuseIdentifier:@"SmallDetailKindCollectionCell"];
    [_smallCollection registerClass:[SmallDetailKindCollectionCellS class] forCellWithReuseIdentifier:@"SmallDetailKindCollectionCellS"];
    
    [_smallCollection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"headerView1"];
    [_smallCollection registerClass:[BigDetailKindHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"BigDetailKindHeaderView"];
}

#pragma mark - 返回item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }
    else if (section >= 1 && section <= _arrModel.count){
        CellDetailKindModel *model = _arrModel[section - 1];
        return model.list.count;
    }
    else  {
        return _navArr.count;
    }
}

#pragma mark - 返回section个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _arrModel.count + 2;
}

#pragma mark - 创建CollectionViewCell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.section == 0) {
        SmallDetailKindCollectionCellA *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDetailKindCollectionCellA" forIndexPath:indexPath];
        if (_topArr.count) {
            CellDetailTopKindModel *model = _topArr[0];
            Cell.titleLabel.text = model.title;
            Cell.contentLabel.text = model.subtitle;
            
            Cell.backgroundColor = [UIColor whiteColor];
        }
        else {
            Cell.titleLabel.text = @"今日暂无排行";
            Cell.contentLabel.text = @"无";
            Cell.backgroundColor = [UIColor whiteColor];
        }
        return Cell;
    }
    if (indexPath.section >= 1 && indexPath.section <= _arrModel.count) {
        SmallDetailKindCollectionCell *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDetailKindCollectionCell" forIndexPath:indexPath];
        CellDetailKindModel *model1 = _arrModel[indexPath.section - 1];
        CellArrDetailKindModel *model2 = model1.list[indexPath.row];
        Cell.cellArrDetailKindModel = model2;
        Cell.backgroundColor = [UIColor whiteColor];
        return Cell;
    }
    else {
        SmallDetailKindCollectionCellS *Cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SmallDetailKindCollectionCellS" forIndexPath:indexPath];
        MoreDetailKindModel *model = _navArr[indexPath.row];
        Cell.moreDetailKindModel = model;
        return Cell;
    }
}

#pragma mark - CollectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (_topArr.count > 0) {
            CellDetailTopKindModel *model = _topArr.lastObject;
            [self.delegate top50PushNextVcWithContryType:model.contentType Key:model.key isKind:1];
        }
    }
    if (indexPath.section >= 1 && indexPath.section <= _arrModel.count) {
        CellDetailKindModel *model1 = _arrModel[indexPath.section - 1];
        CellArrDetailKindModel *model2 = model1.list[indexPath.row];
        [self.delegate pushNextViewControllerWithHttpId:model2.albumId];
    }
    if (indexPath.section == _arrModel.count + 1) {
        [self.delegate contentOffSetWithCurrentIndex:indexPath.row];
    }
}

#pragma mark - 头视图大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return CGSizeMake(SCREEN_WIDTH, 200);
    }
    else {
        return CGSizeMake(SCREEN_WIDTH, 50);
    }
}

#pragma mark - item大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section > _arrModel.count) {
        return CGSizeMake((SCREEN_WIDTH - 4) / 3, 38);
    }
    return CGSizeMake(SCREEN_WIDTH - 2, 78);
}

#pragma mark - 创建头视图
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (kind == UICollectionElementKindSectionHeader) {
            self.header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"headerView1" forIndexPath:indexPath];
            [self createScrollPic];
            return _header;
        }
    }
    else {
        if (kind == UICollectionElementKindSectionHeader) {
            BigDetailKindHeaderView *header1 = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"BigDetailKindHeaderView" forIndexPath:indexPath];
            if (indexPath.section > 0 && indexPath.section < _arrModel.count + 1) {
                
                CellDetailKindModel *model = _arrModel[indexPath.section - 1];
                /** 获取id 传至给下一页面 */
                self.currentId = model.moduleType;
                self.tagName = model.tagName;
            header1.titlelabel.text = [NSString stringWithFormat:@"♦️%@", model.title];
            header1.moreButton.hidden = NO;
            [header1.moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
            }
            else {
                header1.titlelabel.text = @"更多类别";
                header1.moreButton.hidden = YES;
            }
            return header1;
        }
    }
    return nil;
}

#pragma mark - button点击方法
- (void)moreButtonAction:(UIButton *)button {
    [self.delegate clickButtonWithCurrentId:self.currentId TagName:self.tagName];
}

- (void)createScrollPic {
    
    [_picView removeFromSuperview];
    
    NSArray *UrlStringArray = [NSArray arrayWithArray:_scrollArr];
    self.picView = [DCPicScrollView picScrollViewWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 200) WithImageUrls:UrlStringArray];
    [_header addSubview:_picView];
    _picView.AutoScrollDelay = 3;
    [[DCWebImageManager shareManager] setDownloadImageRepeatCount:1];
    [[DCWebImageManager shareManager] setDownLoadImageError:^(NSError *error, NSString *string) {
        
    }];
    _picView.imageViewDidTapAtIndex = ^(NSInteger index) {
        
    };

}

@end
